import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class BookTile extends StatelessWidget {
  final List? books;
  final double? height;
  final double? width;
  const BookTile({super.key, this.books, this.height, this.width});

  @override
  Widget build(BuildContext context) {
    return //特别为你准备
        Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 特别为你准备标题
        _buildRecommendTitle(),
        5.verticalSpace,
        // RecommendSwiper
        _buildRecommendSwiper(context),
      ],
    );
  }

  Widget _buildRecommendSwiper(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Row(
        children: List.generate(books?.length ?? 0, (index) {
          return Padding(
            padding: const EdgeInsets.only(right: 15.0),
            child: Column(
              children: [
                Stack(
                  // 封面
                  children: [
                    Container(
                      height: height,
                      width: width,
                      decoration: BoxDecoration(
                        borderRadius: BorderRadius.circular(10.r),
                        image: DecorationImage(
                          fit: BoxFit.cover,
                          image: CachedNetworkImageProvider(
                            books![index]['image'],
                            headers: const {
                              "User-Agent":
                                  "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
                            },
                          ),
                        ),
                      ),
                    ),
                    // 价格
                    Positioned(
                      bottom: height! / 3,
                      child: Container(
                        width: 120.w,
                        height: 15.h,
                        decoration: BoxDecoration(
                          color: Theme.of(context).colorScheme.primary,
                          borderRadius: BorderRadius.only(
                            topRight: Radius.circular(10.r),
                            bottomRight: Radius.circular(10.r),
                          ),
                        ),
                        child: Text(
                          "￥12.00",
                          style: TextStyle(
                              color: Theme.of(context).colorScheme.onPrimary,
                              fontSize: 18.sp,
                              fontWeight: FontWeight.bold),
                        ),
                      ),
                    ),
                    // 标题
                  ],
                ),
                Container(
                  padding: EdgeInsets.only(top: 10.h),
                  width: width,
                  child: Text(
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis, //超出部分省略
                    books![index]['title'],
                    style: TextStyle(
                      fontSize: 22.sp,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
                // 副标题
                Container(
                  padding: EdgeInsets.only(top: 5.h),
                  width: width,
                  child: Text(
                    maxLines: 1,
                    books![index]['author'],
                    style: TextStyle(
                      fontSize: 18.sp,
                      fontWeight: FontWeight.w500,
                      color: Theme.of(context).colorScheme.inversePrimary,
                    ),
                  ),
                )
              ],
            ),
          );
        }),
      ),
    );
  }

  Widget _buildRecommendTitle() {
    return Text(
      "特别为你准备",
      style: TextStyle(fontSize: 24.sp, fontWeight: FontWeight.w600),
    );
  }
}
